﻿using ZYCommon;
using System;
using System.Xml;
using System.Net;
using Bifrost;
using TextEditor.TextDocument.Document;

namespace TextEditor
{
    

    public class ZYTextSaveLog
    {
        private string dtSaveDateTime = string.Empty;
        public int MarkLevel = 0;
        private ZYTextDocument myOwnerDocument = null;
        private string strUserName = Dns.GetHostName();//null**********************;

        public ZYTextSaveLog()
        {
            if (App.UserAccount != null)
            {
                strUserName = App.UserAccount.UserInfo.User_name;
            }
        }

        public string DisplayText()
        {
            return (this.strUserName + " 保存于" + this.dtSaveDateTime);
        }

        public bool FromXML(XmlElement myElement)
        {
            if (myElement != null)
            {
                this.strUserName = myElement.GetAttribute("name");
                this.dtSaveDateTime = myElement.GetAttribute("time");
                this.UserIP = myElement.GetAttribute("ip");//**************从XML中<savelog>结点属性中获取IP值
                if (myElement.Attributes["user_name"] != null)
                {
                    this.username = myElement.Attributes["user_name"].Value;
                }
                if (myElement.Attributes["user_id"] != null)
                {
                    this.userid = myElement.Attributes["user_id"].Value;
                }
                return true;
            }
            return false;
        }

        private string username = "";
        public string User_name
        {
            set
            {
                username = value;
            }

            get
            {
                if (username == "")
                    return App.UserAccount.UserInfo.User_name;
                return username;
            }
        }

        private string userid = "";
        public string User_id
        {
            set
            {
                userid = value;
            }

            get
            {
                if (userid == "")
                    return App.UserAccount.UserInfo.User_id;
                return userid;
            }
        }

        public string SaveTime
        {
            get
            {
                if (!string.IsNullOrEmpty(dtSaveDateTime))
                {
                    return dtSaveDateTime;
                }
               
                dtSaveDateTime = App.GetSystemTime().ToString("yyyy-MM-dd HH:mm");               
                return dtSaveDateTime;
            }
        }

        public string GetXMLName()
        {
            return "savelog";
        }

        public bool ToXML(XmlElement myElement)
        {
            if (myElement != null)
            {
                if (App.UserAccount != null && App.UserAccount.UserInfo != null)
                {
                    myElement.SetAttribute("user_id", this.User_id);
                    myElement.SetAttribute("user_name", this.User_name);
                }
                myElement.SetAttribute("name", this.strUserName);
                myElement.SetAttribute("time", SaveTime);
                myElement.SetAttribute("ip", this.userIP);//******************创建XML中IP属性
                return true;
            }
            return false;
        }

        public string DisplayDateTime
        {
            get
            {
                if (string.IsNullOrEmpty(dtSaveDateTime))
                    return DateTime.Now.ToString("yyyy-MM-dd HH:mm");
                return dtSaveDateTime;
            }
        }

        public ZYTextDocument OwnerDocument
        {
            get
            {
                return this.myOwnerDocument;
            }
            set
            {
                this.myOwnerDocument = value;
            }
        }

        public string UserName
        {
            get
            {
                return this.username == "" ? this.strUserName : this.username;
            }
            set
            {
                this.strUserName = value;
            }
        }

        //*****************************
        private string userIP = "127.0.0.1";

        public string UserIP
        {
            get { return userIP; }
            set { userIP = value; }
        }
    }
}

